/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import model.Agenda;
import model.Compromisso;
import model.CompromissoDAO;
import model.Paciente;
import model.Profissional;

import model.Usuario;
import model.UsuarioDAO;

/**
 *
 * @author 0731110
 */
public class TrataCadastroCompromisso extends Comando {

    @Override
    public void executar(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException, IOException, ServletException {
        
        response.setContentType("text/html;charset=UTF-8");

        String nomeCompromisso = request.getParameter("nome");        
        String descricaoCompromisso = request.getParameter("descricao");          
        String dataCompromisso = request.getParameter("data");
        String horaCompromisso = request.getParameter("hora");
        String pacienteId = request.getParameter("paciente");
        String agendaId = request.getParameter("agenda");
        String compareceu = request.getParameter("compareceu");
        String valor = request.getParameter("valor");
        String pagamento = request.getParameter("pagamento");
        
        HttpSession session = request.getSession(false);
        Usuario usuario = (Usuario) session.getAttribute("identifica");
        
            /*paciente*/
        String cpf = request.getParameter("cpf");        
        String rg = request.getParameter("rg");          
        String dataNasc = request.getParameter("dataNasc");
        String endereco = request.getParameter("endereco");
        String telefone = request.getParameter("telefone");
        String celular = request.getParameter("celular");
        String email = request.getParameter("email");
        String nome = request.getParameter("nomepac");
        
        PrintWriter out = response.getWriter();
        try {
            
            
            Paciente paciente = new Paciente(nome, cpf, rg, dataNasc, endereco, telefone, celular, email);
            paciente.setIdPessoa(Integer.parseInt(pacienteId));
            
            Profissional prof = new Profissional(nome, cpf, rg, dataNasc, endereco, telefone, celular, email);
            
            Agenda agen = new Agenda(prof, nome, cpf, rg, dataNasc, endereco, telefone, celular, email);
            agen.setIdAgenda(Integer.parseInt(agendaId));
            
            Compromisso compromisso = new Compromisso(nomeCompromisso, descricaoCompromisso, dataCompromisso, horaCompromisso, paciente,agen,compareceu,valor,pagamento);
                        
            CompromissoDAO ddao = new CompromissoDAO();
            ddao.inserir(compromisso);
            
            response.sendRedirect("compromissoCadastrado.jsp");
       } catch(SQLException ex){
            throw new ServletException(ex);
        }catch (ClassNotFoundException cnfe) {
            throw new ServletException(cnfe);
        }
            out.close();
    }
}
